package message.types.string;

import java.io.IOException;

import message.Message;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;

public abstract class StringMessage extends Message {
  
  private String str_;
  
  /**
   * Constructs a message that contains a string
   * @requires parameters not null
   * @param client      The answerer
   * @param destination The recipient
   * @param str         The string
   */
  
  public StringMessage(Stalker client, Stalker destination, String str) {
    super(client, destination);
    assert str != null;
    str_ = str;
  }
  
  public StringMessage(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    str_ = reader.readString();
    assert str_ != null;
  }
  
  public String getString() {
    return str_;
  }

  public boolean contentEquals(Object obj) {
    if (!(obj instanceof StringMessage)) {
      return false;
    }
    StringMessage that = (StringMessage) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getString().equals(that.getString());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    writer.writeString(str_);
  }
  
}
